package com.ruoyi.training.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.training.dto.MyCertificateInfo;
import com.ruoyi.training.dto.MyGradeInfo;
import com.ruoyi.training.dto.SysUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.Students;
import com.ruoyi.training.service.IStudentsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 学员Controller
 *
 * @author ruoyi
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/students")
public class StudentsController extends BaseController
{
    @Autowired
    private IStudentsService studentsService;

    /**
     * 查询学员列表
     */
    @RequiresPermissions("training:students:list")
    @GetMapping("/list")
    public TableDataInfo list(Students students)
    {
        startPage();
        List<Students> list = studentsService.selectStudentsList(students);
        return getDataTable(list);
    }

    /**
     * 查询内训用户列表
     * lqx
     */
    @GetMapping("/userList")
    public TableDataInfo userList(SysUser sysUser)
    {
        startPage();
        List<SysUserDto> list = studentsService.selectUserList(sysUser);
        return getDataTable(list);
    }

    /**
     * 查询学员成绩
     * lqx
     */
    @GetMapping("/getStudentGradeInfo/{studentId}")
    public TableDataInfo getStudentGradeInfo(@PathVariable("studentId") String studentId)
    {
        startPage();
        List<MyGradeInfo> list = studentsService.getStudentGradeInfo(studentId);
        return getDataTable(list);
    }

    /**
     * 查询学员成绩-后端获取id
     * lqx
     */
    @GetMapping("/getStudentGradeInfo")
    public TableDataInfo getStudentGradeInfo()
    {
        String studentId = SecurityUtils.getLoginUser().getSysUser().getStudentId();
        startPage();
        if (!"".equals(studentId) && studentId != null){
            List<MyGradeInfo> list = studentsService.getStudentGradeInfo(studentId);
            return getDataTable(list);
        }else{
            return null;
        }
    }

    /**
     * 查询学员证书
     * lqx
     */
    @GetMapping("/getStudentCertificateInfo/{studentId}")
    public TableDataInfo getStudentCertificateInfo(@PathVariable("studentId") String studentId)
    {
        startPage();
        List<MyCertificateInfo> list = studentsService.getStudentCertificateInfo(studentId);
        return getDataTable(list);
    }

    /**
     * 查询学员证书-后端获取id
     * lqx
     */
    @GetMapping("/getStudentCertificateInfo")
    public TableDataInfo getStudentCertificateInfo()
    {
        String studentId = SecurityUtils.getLoginUser().getSysUser().getStudentId();
        startPage();
        if (!"".equals(studentId) && studentId != null){
            List<MyCertificateInfo> list = studentsService.getStudentCertificateInfo(studentId);
            return getDataTable(list);
        }else{
            return null;
        }
    }

    /**
     * 导出学员列表
     */
    @RequiresPermissions("training:students:export")
    @Log(title = "学员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Students students)
    {
        List<Students> list = studentsService.selectStudentsList(students);
        ExcelUtil<Students> util = new ExcelUtil<Students>(Students.class);
        util.exportExcel(response, list, "学员数据");
    }

    /**
     * 获取学员详细信息
     */
    @RequiresPermissions("training:students:query")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") String studentId)
    {
        return success(studentsService.selectStudentsByStudentId(studentId));
    }

    /**
     * 新增学员
     */
    @RequiresPermissions("training:students:add")
    @Log(title = "学员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Students students)
    {
        int insertStatus = studentsService.insertStudents(students);
        if (insertStatus == -1)
        {
            return error("新增学员[" + students.getStudentName() + "-" + students.getIdNumber() + "]失败，该证件号已存在");
        }
        return toAjax(insertStatus);
    }

    /**
     * 新增学员-同步用户
     * lqx
     */
    @PostMapping("/addWithUser")
    public AjaxResult addWithUser(@RequestBody Students students)
    {
        int insertStatus = studentsService.insertStudentsWithUser(students);
        if (insertStatus == -1)
        {
            return error("新增学员[" + students.getStudentName() + "-" + students.getIdNumber() + "]失败，该证件号已存在");
        }
        if (insertStatus == -2)
        {
            return error("新增学员[" + students.getStudentName() + "-" + students.getIdNumber() + "]失败，该手机号已存在");
        }
        return toAjax(insertStatus);
    }

    /**
     * 修改学员
     */
    @RequiresPermissions("training:students:edit")
    @Log(title = "学员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Students students)
    {
        int updataStatus = studentsService.updateStudents(students);
        if (updataStatus == -1)
        {
            return error("该证件号已被其他学员使用，请核对后修改");
        }
        return toAjax(updataStatus);
    }

    /**
     * 修改学员-同步用户
     * lqx
     */
    @PutMapping("/editWithUser")
    public AjaxResult editWithUser(@RequestBody Students students)
    {
        int updataStatus = studentsService.updateStudentsWithUser(students);
        if (updataStatus == -1)
        {
            return error("该证件号已被其他学员使用，请核对后修改");
        }
        if (updataStatus == -2)
        {
            return error("该手机号已被其他学员使用，请核对后修改");
        }
        return toAjax(updataStatus);
    }

    /**
     * 删除学员
     */
    @RequiresPermissions("training:students:remove")
    @Log(title = "学员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable String[] studentIds)
    {
        return toAjax(studentsService.deleteStudentsByStudentIds(studentIds));
    }

    /**
     * 删除学员-同步用户
     * lqx
     */
    @DeleteMapping("/removeWithUser/{studentIds}")
    public AjaxResult removeWithUser(@PathVariable String[] studentIds)
    {
        return toAjax(studentsService.deleteStudentsWithUserByStudentIds(studentIds));
    }
}
